package cn.qn.controller;

import cn.qn.common.util.UserSubjectUtil;
import cn.qn.dto.ResponseDTO;
import cn.qn.dto.user.AppUserAttentionDTO;
import cn.qn.exceptions.BizException;
import cn.qn.moudle.user.AppUser;
import cn.qn.service.IAppUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author: Bryant
 * @date: 2019/7/18
 * @description: 文章管理
 */
@RestController
@RequestMapping("/user/attention")
public class AppUserAttentionController {

    @Autowired
    private IAppUserService appUserService;

    @RequestMapping(value = "/list", name = "我关注的用户列表", method = RequestMethod.GET)
    public ResponseDTO allAttentions(@RequestHeader String authorization) {
        AppUser appUser = UserSubjectUtil.getAppUser(authorization);
        if (null == appUser) {
            return ResponseDTO.error("登录已过期");
        }
        List<AppUserAttentionDTO> attentions = appUserService.queryMyAttentions(appUser.getUserId());
        return ResponseDTO.success(attentions);
    }

    @RequestMapping(value = "/list/3", name = "查询3条我关注的用户", method = RequestMethod.GET)
    public ResponseDTO threeAttentions(@RequestHeader String authorization) {
        AppUser appUser = UserSubjectUtil.getAppUser(authorization);
        if (null == appUser) {
            return ResponseDTO.error("登录已过期");
        }
        List<AppUserAttentionDTO> attentions = appUserService.queryMyThreeAttentions(appUser.getUserId());
        return ResponseDTO.success(attentions);
    }

    @RequestMapping(value = "/attention/{attentionId}", name = "关注用户", method = RequestMethod.POST)
    public ResponseDTO attentionOneUser(@PathVariable(value = "attentionId")String attentionId, @RequestHeader String authorization) {
        AppUser appUser = UserSubjectUtil.getAppUser(authorization);
        try {
            if(appUser.getUserId().equals(attentionId)){
                throw new BizException("用户不能关注自己");
            }
            appUserService.attentionOneUser(appUser.getUserId(),attentionId);
            return ResponseDTO.success();
        } catch (BizException e) {
            return ResponseDTO.error(e.getMessage());
        }
    }

    @RequestMapping(value = "/cancle/{attentionId}", name = "取消关注用户", method = RequestMethod.POST)
    public ResponseDTO cancleAttentionOneUser(@PathVariable(value = "attentionId")String attentionId, @RequestHeader String authorization) {
        AppUser appUser = UserSubjectUtil.getAppUser(authorization);
        try {
            appUserService.cancleAttentionOneUser(appUser.getUserId(),attentionId);
            return ResponseDTO.success();
        } catch (BizException e) {
            return ResponseDTO.error(e.getMessage());
        }
    }
}
